<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('分配班级')"/>
</head>
<body class="gray-bg">
<form class="form-horizontal m" id="form-classes-allotClass">
    <div class="container-div">
        <div class="col-sm-12 search-collapse">
            <form id="export-form">
                <div class="select-list">
                    <ul>
                        <li>
<!--                            <input type="hidden" name="params[classId]" th:each="teachingClassesStudents : ${classesStudents}" th:value="${teachingClassesStudents.classId}">-->
                            <input type="hidden" name="courseId" th:value="${teachingStudentApply.courseId}">
                            班级名称：<input type="text" name="className" value=""/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm"
                               onclick="$.table.search()"><i class="fa fa-search"></i>搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="row">
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var applyId = [[${teachingStudentApply.applyId}]];
    var schoolId = [[${teachingStudentApply.schoolId}]];
    var courseId = [[${teachingStudentApply.courseId}]];
    var studentId = [[${teachingStudentApply.studentId}]];
    // var studentName = $.common.trim($("input[name='studentName']").val());
    var prefix = ctx + "teaching/classes";

    $(function () {
        var options = {
            url: prefix + "/list",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            rememberSelected: true,
            columns: [{
                checkbox: true
            },
                {
                    field: 'classId',
                    title: '班级主键ID',
                    visible: false
                },
                {
                    field: 'className',
                    title: '班级名称'
                },
                {
                    field: 'teacherName',
                    title: '教师',
                },
                {
                    field: 'createTime',
                    title: '报名时间',
                    formatter: function (value, row, index) {
                        return $.common.dateFormat(value);
                    }
                },
                {
                    title: '人数',
                    formatter: function (value, row, index) {
                        return row.classStudent + '/' + row.maxStudent;
                    }
                }
            ]
        };
        $.table.init(options);
    });

    /* 添加用户-选择用户-提交 */
    function submitHandler() {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        allotClass(rows.join());
    }

    function allotClass(){
        var url = ctx + "teaching/students/allotClassSave";
        var classIds = $.table.selectColumns("classId");
        var datas = [];
        $.each(classIds, function (index, value){
            let data = {};
            data.schoolId = schoolId;
            data.classId = value;
            data.studentId = studentId;
            data.applyId = applyId;
            datas.push(data);
        })
        $.ajax({
            method: "post",
            contentType: "application/json",
            url: ctx + "teaching/students/allotClassSave",
            dataType: "json",
            data: JSON.stringify(datas),
            async: false,
            success: function (r){
                if (r.code == 0){
                    refreshTab();
                    $.modal.close();
                    $.modal.alertSuccess("分班成功");
                } else {
                    $.modal.msg(r.msg);
                }
            }
        })
    }
</script>
</body>
</html>